package com.dmooo.ymx.fragments;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;
import com.dmooo.ymx.R;
import com.dmooo.ymx.activity.BaoYouActivity;
import com.dmooo.ymx.activity.DouActivity;
import com.dmooo.ymx.activity.ElemeActivity;
import com.dmooo.ymx.activity.GaoyongActivity;
import com.dmooo.ymx.activity.GuoyedanActivity;
import com.dmooo.ymx.activity.JdActivity;
import com.dmooo.ymx.activity.JiayoulistActivity;
import com.dmooo.ymx.activity.PHBActivity;
import com.dmooo.ymx.activity.PddActivity;
import com.dmooo.ymx.activity.WebViewActivity;
import com.dmooo.ymx.activity.WebViewActivity3;
import com.dmooo.ymx.activity.WphActivity;
import com.dmooo.ymx.activity.ZeroBuyActivity;
import com.dmooo.ymx.base.BaseLazyFragment;
import com.dmooo.ymx.common.SPUtils;
import com.dmooo.ymx.login.WelActivity;
import com.dmooo.ymx.merchantactivity.MerchanthomeActivity;
import com.dmooo.ymx.suning.SuningHomeActivity;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChaoJiDaoHangFragment extends BaseLazyFragment {
    @Override
    protected void lazyload() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_cjdh, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.icon_super_a, R.id.icon_super_b, R.id.icon_super_c, R.id.icon_super_d, R.id.icon_taobao_a, R.id.icon_taobao_b, R.id.icon_taobao_c, R.id.icon_taobao_d, R.id.icon_taobao_e, R.id.icon_taobao_f, R.id.icon_taobao_g, R.id.icon_taobao_h, R.id.icon_taobao_i, R.id.icon_taobao_j, R.id.icon_taobao_k, R.id.icon_taobao_l, R.id.icon_taobao_m, R.id.icon_life_h, R.id.icon_taobao_n, R.id.icon_taobao_o, R.id.icon_taobao_p, R.id.icon_life_a, R.id.icon_life_b, R.id.icon_life_c, R.id.icon_life_d, R.id.icon_life_e, R.id.icon_life_f, R.id.icon_life_g})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.icon_super_a:
                openActivity(PddActivity.class);
                break;
            case R.id.icon_super_b:
                openActivity(JdActivity.class);
                break;
            case R.id.icon_super_c:
                openActivity(WphActivity.class);
                break;
            case R.id.icon_super_d:
                openActivity(SuningHomeActivity.class);
                break;
            case R.id.icon_taobao_a:
                intent = new Intent(getActivity(), BaoYouActivity.class);
                intent.putExtra("type", "9");
                startActivity(intent);
                break;
            case R.id.icon_taobao_b:
                intent = new Intent(context, WebViewActivity3.class);
                intent.putExtra("title", "天猫超市");
                intent.putExtra("url", "https://chaoshi.tmall.com/?targetPage=index");
                startActivity(intent);
                break;
            case R.id.icon_taobao_c:
                intent = new Intent(context, WebViewActivity3.class);
                intent.putExtra("title", "天猫美妆");
                intent.putExtra("url", "https://meizhuang.tmall.com");
                startActivity(intent);
                break;
            case R.id.icon_taobao_d:
                intent = new Intent(context, WebViewActivity3.class);
                intent.putExtra("title", "天猫国际");
                intent.putExtra("url", "https://pages.tmall.com/wow/jinkou/act/zhiyingchaoshi?from=zebra:offline");
                startActivity(intent);
                break;
            case R.id.icon_taobao_e:
                intent = new Intent(getActivity(), BaoYouActivity.class);
                intent.putExtra("type", "20");
                startActivity(intent);
                break;
            case R.id.icon_taobao_f:
                intent = new Intent(getActivity(), BaoYouActivity.class);
                intent.putExtra("type", "5");
                startActivity(intent);
                break;
            case R.id.icon_taobao_g:
                intent = new Intent(getActivity(), BaoYouActivity.class);
                intent.putExtra("type", "22");
                startActivity(intent);
                break;
            case R.id.icon_taobao_h:
                if ("".equals(SPUtils.getStringData(context, "token", ""))) {
                    openActivity(WelActivity.class);
                    return;
                }
                openActivity(ZeroBuyActivity.class);
                break;
            case R.id.icon_taobao_i:
                openActivity(PHBActivity.class);
                break;
            case R.id.icon_taobao_j:
                openActivity(PinPaiFragment.class);
                break;
            case R.id.icon_taobao_k:
                openActivity(GuoyedanActivity.class);
                break;
            case R.id.icon_taobao_l:
                intent = new Intent(getActivity(), GaoyongActivity.class);
                intent.putExtra("url", "/app.php?c=Zhetaoke&a=getBaodanGoods");
                intent.putExtra("title", "捡漏神单");
                intent.putExtra("type", "2");
                startActivity(intent);
                break;
            case R.id.icon_taobao_m:
                break;
            case R.id.icon_life_h:
                intent = new Intent(getActivity(), BaoYouActivity.class);
                intent.putExtra("type", "23");
                startActivity(intent);
                break;
            case R.id.icon_taobao_n:
                intent = new Intent(context, WebViewActivity3.class);
                intent.putExtra("title", "飞猪旅行");
                intent.putExtra("url", "https://h5.m.taobao.com/trip/home/index.html");
                startActivity(intent);
                break;
            case R.id.icon_taobao_o:
                intent = new Intent(getActivity(), BaoYouActivity.class);
                intent.putExtra("type", "4");
                startActivity(intent);
                break;
            case R.id.icon_taobao_p:
                Bundle bundle = new Bundle();
                bundle.putString("id", "0");
                openActivity(DouActivity.class, bundle);
                break;
            case R.id.icon_life_a:
                openActivity(ElemeActivity.class);
                break;
            case R.id.icon_life_b:
                Acp.getInstance(context).request(new AcpOptions.Builder()
                        .setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                        .build(), new AcpListener() {
                    @Override
                    public void onGranted() {
                        openActivity(MerchanthomeActivity.class);
                    }

                    @Override
                    public void onDenied(List<String> permissions) {
                        showToast("需要您的定位权限");
                    }
                });
                break;
            case R.id.icon_life_c:
                intent = new Intent(context, WebViewActivity3.class);
                intent.putExtra("title", "生活券");
                long timeStamp = System.currentTimeMillis();
                String signstr = "agentId=405&machineCode="+SPUtils.getStringData(context,"uid","1")+"&timestamp="+timeStamp;
                String singstrs = signstr+"&secretKey=Pe7HRBJYwQfpxCb3s5TGW3hXeWxhXKpH";
                String sign = md5( singstrs );
                intent.putExtra("url", "http://tq.365taoquan.cn/seller/app/classify?machineCode="+SPUtils.getStringData(context,"uid","1")+"&agentId=405&timestamp="+timeStamp+"&sign="+sign);
                startActivity(intent);
                break;
            case R.id.icon_life_d:
                intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("title", "充值");
                intent.putExtra("url","http://app.yangkeduo.com/deposit.html");
                startActivity(intent);
                break;
            case R.id.icon_life_e:
                if ("".equals(SPUtils.getStringData(context, "token", ""))) {
                    openActivity(WelActivity.class);
                    return;
                }
                Acp.getInstance(context).request(new AcpOptions.Builder()
                        .setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                        .build(), new AcpListener() {
                    @Override
                    public void onGranted() {
                        openActivity(JiayoulistActivity.class);
                    }

                    @Override
                    public void onDenied(List<String> permissions) {
                        showToast("加油站需要您的定位权限");
                    }
                });
                break;
            case R.id.icon_life_f:
                intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("title", "洗车");
                intent.putExtra("url", "https://wash.chengniu.com/wash/#/home?systemCode=CPH5&activityChannel=zxyd&orderChannel=59");
                startActivity(intent);
                break;
            case R.id.icon_life_g:
                if ("".equals(SPUtils.getStringData(context, "token", ""))) {
                    openActivity(WelActivity.class);
                    return;
                }
                intent = new Intent(context, WebViewActivity3.class);
                intent.putExtra("title", "美团酒店");
                intent.putExtra("url", "https://runion.meituan.com/url?a=1&key=ebf5da1e1cd2073df1e538618fca245c348&url=https://i.meituan.com/awp/h5/hotel/search/search.html?cevent=imt%2Fhomepage%2Fcategory1%2F20&sid=hkx_"+SPUtils.getStringData(context,"uid",""));
                startActivity(intent);
                break;
        }
    }
    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result.toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
